package cn.tecnova.utils

import java.sql.{Connection, DriverManager, PreparedStatement, ResultSet}

/**
  * description:广播变量工具类
  **/
object BroadcastTools {

  /**
    * 获取媒体库数据,并将数据装入map
    */
  def getMediaDic(): Map[String, String]= {

    var broadConn: Connection = null
    var mediaStat: PreparedStatement = null

    var mediaList = List[(String, String)]()

    try {

      broadConn = DriverManager.getConnection(ConfigHandler.url, ConfigHandler.user, ConfigHandler.passwd)

      //读取媒体表
      mediaStat = broadConn.prepareStatement(
        """
          select
          id,media
          from media_type_dict
        """.stripMargin)

      val mediaTypeSet: ResultSet = mediaStat.executeQuery()

      while (mediaTypeSet.next()) {

        val mediaTypeId = mediaTypeSet.getString("id")
        val mediaTypeName = mediaTypeSet.getString("media")
        mediaList :+= (mediaTypeId, mediaTypeName)

      }

    } catch {
      case e: Exception => e.printStackTrace()
    } finally {

      if (mediaStat != null) mediaStat.close()
      if (broadConn != null) broadConn.close()

    }

    //将媒体数据转换成map类型
    val map: Map[String, String] = mediaList.toMap
    map
  }

}
